<?php 
class ControllerProductCategories extends Controller {  
	public function index() { 
		$this->language->load('product/categories');  		
		$this->load->model('catalog/category');
		$this->load->model('tool/seo_url');  
		
        $this->data['template'] = $this->config->get('config_template');  
        $this->data['sub_category'] = false; 
        $this->data['category_level'] = 0;      
        $this->document->breadcrumbs = array();        
        
		if (isset($this->request->get['path'])) {
			$path = $this->request->get['path'];		
            $parts = explode('_', $path);
			$breadcrumbs = explode('_', $path);                        
			$category_id = array_pop($parts); 
            $parent_path = str_replace($category_id, "", $path);
            if(strlen($parent_path) > 0){
                $parent_path = '&amp;path=' . substr($parent_path, 0, strlen($parent_path) - 1);
            }
            
            $this->data['back'] = $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/categories' . $parent_path);
            $this->data['path'] = $category_id; 
            $this->data['category_level'] = count($parts);     
            
            // Get all children            
            if(count($parts) > 0){
                $categories = $this->model_catalog_category->getCategories($category_id);     
                $this->data['sub_category'] = true;  
                $this->document->breadcrumbs = $this->model_catalog_category->createBreadcrumbs($breadcrumbs);
                /*
                $breadcrumb_path = '';
                foreach($breadcrumbs as $breadcrumb){
                    $separator = $breadcrumb_path ? ' >> '  : false;
                    $breadcrumb_path = $breadcrumb_path ? ($breadcrumb_path . '_' . $breadcrumb) : $breadcrumb;
                    
                    $this->document->breadcrumbs[] = array(
                        'href'      => $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/categories&path=' . $breadcrumb_path),
                        'text'      => $this->model_catalog_category->getCategoryName($breadcrumb),
                        'separator' => $separator,
                    );
                }
                */                    
            }
            else{
                //$categories = $this->model_catalog_category->getDisableCategories($category_id); 
                $url = $this->model_tool_seo_url->rewrite(HTTPS_SERVER . 'index.php?route=product/manufacturer/category&path=' . $path);
                $this->redirect($url);
            }
		} 
        else {
			$category_id = 0;
            $this->data['back'] = $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=common/home');
            $this->data['path'] = '';
            
            // Get all children
            $categories = $this->model_catalog_category->getCategories($category_id);
		}
        $this->data['back_product'] = $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/categories');		           
        $this->data['breadcrumbs'] = $this->document->breadcrumbs;  
        $this->data['giftcard_link'] = $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=checkout/giftcard');
		
        // Get current category
        $category_info = $this->model_catalog_category->getCategory($category_id);  
        
		if ($categories){                  
            if($category_info){
                $page_title = $category_info['meta_title'] ? $category_info['meta_title'] : $category_info['name'];  
            }
            else{
                $page_title = $this->language->get('text_meta-title');
            }   
	  		$this->document->title = $page_title;			
			$this->document->keywords = $category_info ? $category_info['meta_keywords'] : $this->config->get('config_meta_keywords');          			
			$this->document->description = $category_info ? $category_info['meta_description'] : $this->config->get('config_meta_description');	
        
			$this->data['heading_title'] = $category_info ? $category_info['name'] : $this->language->get('text_categories');			
			$this->data['description'] = $category_info ? html_entity_decode($category_info['description'], ENT_QUOTES, 'UTF-8') : '';			
			$this->data['text_sort'] = $this->language->get('text_sort');      			
			
			if (isset($this->request->get['page'])) {
				$page = $this->request->get['page'];
			} 
            else { 
				$page = 1;
			}				
			if (isset($this->request->get['sort'])) {
				$sort = $this->request->get['sort'];
			} 
            else {
				$sort = 'p.sort_order';
			}  
			if (isset($this->request->get['order'])) {
				$order = $this->request->get['order'];
			} 
            else {
				$order = 'ASC';
			}

			$url = '';  			
			if (isset($this->request->get['sort'])) {
				$url .= '&sort=' . $this->request->get['sort'];
			}
			if (isset($this->request->get['order'])) {
				$url .= '&order=' . $this->request->get['order'];
			}    			
            
            $this->load->model('catalog/product');  
            $this->load->model('tool/image');             
            
            $this->data['categories'] = array();                 
            foreach ($categories as $result) {
                if ($result['image']) {
                    $image = $result['image'];
                } 
                else {
                    $image = 'no_image.jpg';
                }
                
                if(!isset($this->request->get['path'])){
                    $new_path = $result['category_id'];                   
                } 
                else{
                    $new_path = $this->request->get['path'] . '_' . $result['category_id'];
                } 
                                
                $children = $this->model_catalog_category->getTotalCategoriesByCategoryId($result['category_id']);  
                if($children){
                    $link = HTTP_SERVER . 'index.php?route=product/categories&amp;path=' . $new_path . $url;
                }            
                else{
                    $link = HTTP_SERVER . 'index.php?route=product/category&amp;path=' . $new_path . $url;    
                } 
                
				//$shortname = (mb_strlen($result['name']) <= 18) ? $result['name'] : mb_substr($result['name'], 0, 17) . ' ...';				
                $this->data['categories'][] = array(
                    'name'  	        => $result['name'],
                    'href'  	        => $this->model_tool_seo_url->rewrite($link),
                    'image'           	=> $this->model_tool_image->resize($image, 137, 167),
                    //'image'             => $this->model_tool_image->resize($image, 214, 50),
                    'meta_keywords'     => $result['meta_keywords'],
                    'meta_description'  => $result['meta_description'],
                  );
            }                    			
			   
        	if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/product/categories.tpl')) {
                $this->template = $this->config->get('config_template') . '/template/product/categories.tpl';
            } 
            else {
                $this->template = 'default/template/product/categories.tpl';
            }    
            	
			$this->children = array(
				'common/header',
                'common/footer',
				'common/column_left',							
			);   	
			$this->response->setOutput($this->render(TRUE), $this->config->get('config_compression'));
    	} 
        else {
			$url = '';
			
			if (isset($this->request->get['sort'])) {
				$url .= '&sort=' . $this->request->get['sort'];
			}
			if (isset($this->request->get['order'])) {
				$url .= '&order=' . $this->request->get['order'];
			} 				
			if (isset($this->request->get['page'])) {
				$url .= '&page=' . $this->request->get['page'];
			}			
			if (isset($this->request->get['path'])) {	
	       		$this->document->breadcrumbs[] = array(
   	    			'href'      => $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/category&path=' . $this->request->get['path'] . $url),
    	   			'text'      => $this->language->get('text_error'),
        			'separator' => $this->language->get('text_separator')
        		);
			}
				
			$this->document->title = $this->language->get('text_error');
      		$this->data['heading_title'] = $this->language->get('text_error');    
      		$this->data['text_error'] = $this->language->get('text_error_desc');  
      		$this->data['button_continue'] = $this->language->get('button_continue'); 
            $this->data['breadcrumbs'] = $this->document->breadcrumbs; 
            
      		$this->data['continue'] = null; //HTTP_SERVER . 'index.php?route=common/home';
			//$this->data['continue'] = $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/categories');
            //$this->data['image_name'] = 'bt_tibagetilprodukter.png'; 
            
			if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/error/not_found.tpl')) {
				$this->template = $this->config->get('config_template') . '/template/error/not_found.tpl';
			} 
            else {
				$this->template = 'default/template/error/not_found.tpl';
			}	
			
			$this->children = array(
				'common/header',
                'common/footer',
                'common/column_left',
			);  		
			$this->response->setOutput($this->render(TRUE), $this->config->get('config_compression'));
		}
  	}
}
?>